﻿#region

using System.Collections.Generic;
using System.Linq;
using Tateeda.ClinicalStudy.Base.Data.DBModel;
using Tateeda.ClinicalStudy.Base.Modules.Models.Medication;

#endregion

#region - Legal -

///<legal>
/// Copyright © 2011, Tateeda Media Network
/// All rights reserved. http://tateeda.com
///
/// Redistribution and use in source and binary forms, with or without
/// modification, are permitted provided that the following conditions
/// are met:
///
/// - Redistributions of source code must retain the above copyright
/// notice, this list of conditions and the following disclaimer.
///
/// - Neither the name of the Tateeda Media Network, nor the names of its
/// contributors may be used to endorse or promote products
/// derived from this software without specific prior written
/// permission.
///
/// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
/// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
/// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
/// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
/// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
/// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES INCLUDING,
/// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
/// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
/// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
/// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
/// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
/// POSSIBILITY OF SUCH DAMAGE.
///</legal>

#endregion - Legal -

namespace Tateeda.ClinicalStudy.Base.Modules.Common.Extenstions {

    public static class MedicationExtension {

        public static Drug ToDrug(this DrugModel drug) {
            var to = new Drug();
            To(drug, ref to);
            return to;
        }

        public static DrugCategory ToDrugCategory(this DrugCategoryModel drugCategory) {
            var to = new DrugCategory();
            To(drugCategory, ref to);
            return to;
        }

        public static ICollection<DrugCategory> ToDrugCategoryList(this ICollection<DrugCategoryModel> drugCategories) {
            return drugCategories.Select(d => d.ToDrugCategory()).ToList();
        }

        public static DrugCategoryModel ToDrugCategoryModel(this DrugCategory drugCategory) {
            var to = new DrugCategoryModel();
            To(drugCategory, ref to);
            return to;
        }

        public static ICollection<DrugCategoryModel> ToDrugCategoryModelList(this ICollection<DrugCategory> drugCategories) {
            return drugCategories.Select(d => d.ToDrugCategoryModel()).ToList();
        }

        public static DrugClass ToDrugClass(this DrugClassModel drugClass) {
            var to = new DrugClass();
            To(drugClass, ref to);
            return to;
        }

        public static ICollection<DrugClass> ToDrugClasslList(this ICollection<DrugClassModel> drugClasses) {
            return drugClasses.Select(d => d.ToDrugClass()).ToList();
        }

        public static DrugClassModel ToDrugClassModel(this DrugClass drugClass) {
            var to = new DrugClassModel();
            To(drugClass, ref to);
            return to;
        }

        public static ICollection<DrugClassModel> ToDrugClassModelList(this ICollection<DrugClass> drugClasses) {
            return drugClasses.Select(d => d.ToDrugClassModel()).ToList();
        }

        public static ICollection<Drug> ToDruglList(this ICollection<DrugModel> drugs) {
            return drugs.Select(d => d.ToDrug()).ToList();
        }

        public static DrugModel ToDrugModel(this Drug drug) {
            var to = new DrugModel();
            To(drug, ref to);
            return to;
        }

        public static ICollection<DrugModel> ToDrugModelList(this ICollection<Drug> drugs) {
            return drugs.Select(d => d.ToDrugModel()).ToList();
        }

        public static RecreationalDrugOrSubstance ToSubstance(this RecreationalDrugOrSubstanceModel substanceModal) {
            var to = new RecreationalDrugOrSubstance();
            To(substanceModal, ref to);
            return to;
        }

        public static ICollection<RecreationalDrugOrSubstance> ToSubstanceList(
            this ICollection<RecreationalDrugOrSubstanceModel> substanceModel) {
            return substanceModel.Select(d => d.ToSubstance()).ToList();
        }

        public static RecreationalDrugOrSubstanceModel ToSubstanceModel(this RecreationalDrugOrSubstance substance) {
            var to = new RecreationalDrugOrSubstanceModel();
            To(substance, ref to);
            return to;
        }

        public static ICollection<RecreationalDrugOrSubstanceModel> ToSubstanceModelList(
            this ICollection<RecreationalDrugOrSubstance> substance) {
            return substance.Select(d => d.ToSubstanceModel()).ToList();
        }

        public static ICollection<SubjectDrugModel> ToSubjectDrugModelList(this ICollection<SubjectDrug> drugs) {
            return drugs.Select(d => d.ToSubjectDrugModel()).ToList();
        }

        public static SubjectDrugModel ToSubjectDrugModel(this SubjectDrug drug) {
            var to = new SubjectDrugModel();
            To(drug, ref to);
            if (drug.Drug != null) {
                to.IsSychotropic = drug.Drug.DrugClass.DrugCategoryId == Constants.DefaultPsycotropicCategory;
                to.Name = drug.Drug.Name;
            }
            return to;
        }

        public static ICollection<SubjectDrug> ToSubjectDrugList(this ICollection<SubjectDrugModel> drugsModel) {
            return drugsModel.Select(d => d.ToSubjectDrug()).ToList();
        }

        public static SubjectDrug ToSubjectDrug(this SubjectDrugModel drugModel) {
            var to = new SubjectDrug();
            To(drugModel, ref to);
            return to;
        }

        #region - Base function for Objects conversions -

        private static void To<TF, T>(TF from, ref T to) {
            ExtensionBase.To(from, ref to);
        }

        #endregion - Base function for Objects conversions -
    }
}